package org.free.learn.htmlparser;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.htmlparser.Node;
import org.htmlparser.Parser;

import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.visitors.TextExtractingVisitor;

import java.io.IOException;
import java.net.URL;

/**
 * User: 缪凯
 * Date: 11-10-31
 * Time: 上午8:33
 */
public class ParserPage {
    public String showPageString(String url){
        String html = null;
        if(url != null && !"".equals(url)){
            try {
                //根据url获取页面
                Parser par = new Parser((new URL(url)).openConnection());
                TextExtractingVisitor visitor = new TextExtractingVisitor();
                par.visitAllNodesWith(visitor);
                html = visitor.getExtractedText();
            } catch (ParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return html;
    }

    public Parser getHTMLParser(String url){
        Parser parser = null;
        if(url != null && !"".equals(url)){
            try {
                parser = new Parser(new URL(url).openConnection());
            } catch (ParserException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return parser;
    }


    public static void main(String[] args){
        ParserPage pp = new ParserPage();
        int page = 111;
        String url = "http://www.js-lottery.com/syxw/syxwlssj/@.html";
        String sPage = "";
        for(int p = 0 ; p < 120 ; p++){
            sPage = url.replaceAll("@" , String.valueOf(page + p));
            Parser parser = pp.getHTMLParser(sPage);

            try {
                NodeList nodes = parser.extractAllNodesThatMatch(new TagNameFilter("table"));

                for(int i = 0 ; i < nodes.size() ; i++){
                    System.out.println("这是第"+i+"个table其中包含的文字有："+nodes.elementAt(i).toPlainTextString());
                    //tr队列
                    NodeList trlist = new NodeList();
                    nodes.elementAt(i).collectInto(trlist , new TagNameFilter("tr"));
                    for(int j = 1 ; j < trlist.size() ; j++){
                        //span节点队列
                        NodeList spanList = new NodeList();
                        trlist.elementAt(j).collectInto(spanList , new TagNameFilter("span"));
                        for(int k = 0 ; k < spanList.size() ; k++){
//                            System.out.print(spanList.elementAt(k).toPlainTextString() + "@");
                        }
                    }
                }
            } catch (ParserException e) {
                e.printStackTrace();
            }
        }

    }
}
